package com.gedmanager.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class StoreDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.STORE_ID, MySQLiteHelper.STORE_NAME,
			MySQLiteHelper.STORE_PRICE, MySQLiteHelper.STORE_DATE, MySQLiteHelper.STORE_URL,
			MySQLiteHelper.STORE_AMOUNT };

	public StoreDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Store createStore(String name, String price, long date, String image_url, long amount) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.STORE_NAME, name);
		values.put(MySQLiteHelper.STORE_PRICE, price);
		values.put(MySQLiteHelper.STORE_DATE, date);
		values.put(MySQLiteHelper.STORE_URL, image_url);
		values.put(MySQLiteHelper.STORE_AMOUNT, amount);
		long insertId = database.insert(MySQLiteHelper.TABLE_STORE, null, values);
		Cursor cursor = database.query(MySQLiteHelper.TABLE_STORE, allColumns,
				MySQLiteHelper.STORE_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Store newStore = cursorToStore(cursor);
		cursor.close();
		return newStore;
	}

	public void deleteStore(long id) {
		//long id = store.getId();
		System.out.println("Store deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_STORE, MySQLiteHelper.STORE_ID + " = " + id, null);
	}

	public void updateStore(Store store) {

		ContentValues values = new ContentValues();
		//values.put(MySQLiteHelper.STORE_ID, store.getId());
		values.put(MySQLiteHelper.STORE_NAME, store.getName());
		values.put(MySQLiteHelper.STORE_PRICE, store.getPrice());
		values.put(MySQLiteHelper.STORE_DATE, store.getCreationDate());
		values.put(MySQLiteHelper.STORE_URL, store.getImage_url());
		values.put(MySQLiteHelper.STORE_AMOUNT, store.getAmount());
		System.out.println("Store updated with id: " + store.getId());

		Log.d("aqui update: ",
				((Long)store.getId()).toString());
		database.update(MySQLiteHelper.TABLE_STORE, values, MySQLiteHelper.STORE_ID + "=" + ((Long)store.getId()).toString(), null);


	}

	public List<Store> getAllStores() {
		List<Store> stores = new ArrayList<Store>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_STORE, allColumns, null, null, null,
				null, MySQLiteHelper.STORE_DATE + " DESC");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Store store = cursorToStore(cursor);
			stores.add(store);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return stores;
	}

	@SuppressWarnings("unused")
	public Store getStore(long id) {
		try {
			Log.d("getstore", "inicio");
			Cursor cursor = database.query(MySQLiteHelper.TABLE_STORE, allColumns,
					MySQLiteHelper.STORE_ID + "=?", new String[] { ((Long) id).toString() }, null,
					null, null);
			Log.d("getstore dec", cursor.toString());
			if (cursor != null) {
				cursor.moveToFirst();
				Log.d("getstore dec", cursor.getString(1));
			}
			else { 
				return null;
			}

			Store store = cursorToStore(cursor);
			// make sure to close the cursor
			cursor.close();
			Log.d("getstore", "finish");
			return store;
		}
		catch(Exception e) {
			e.printStackTrace();
		}

		return null;

	}

	public List<Store> getSubsetStores(Date start, Date end) {
		List<Store> stores = new ArrayList<Store>();

		Log.d("aqui: ", "select ..");
		Cursor cursor = database.query(
				MySQLiteHelper.TABLE_STORE,
				allColumns,
				MySQLiteHelper.STORE_DATE + "<?" + " AND " + MySQLiteHelper.STORE_DATE + ">?",
				new String[] { ((Long) end.getTime()).toString(),
						((Long) start.getTime()).toString() }, null, null,
						MySQLiteHelper.STORE_DATE + " DESC");

		// Cursor cursor = database.rawQuery(sql, null);

		Log.d("aqui: ",
				"cursos .." + ((Long) end.getTime()).toString() + " "
						+ ((Long) start.getTime()).toString());
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Store store = cursorToStore(cursor);
			stores.add(store);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return stores;
	}

	private Store cursorToStore(Cursor cursor) {
		Store store = new Store();
		store.setId(cursor.getLong(0));
		store.setName(cursor.getString(1));
		store.setPrice(cursor.getString(2));
		store.setCreationDate(cursor.getLong(3));
		store.setImage_url(cursor.getString(4));
		store.setAmount(cursor.getLong(5));

		Log.d("aqui no cursor: ", ((Long) cursor.getLong(5)).toString());
		return store;
	}
}